$(function(){
		
	/* Tools options */
	$('.tools_toogle').click(function(){
		if(parseInt($('.tools').css('left')) < 0)
		{
			$('.tools').animate({ left: '0' }, 600, 'easeOutQuint');
			$('.tools_toogle').css({'background-position':'0px -58px'});
		}
		else
		{
			$('.tools').animate({ left: '-192px' }, 600, 'easeOutQuint');
			$('.tools_toogle').css({'background-position':'0px 0px'});
		}
	});
		
	/* Register box */
	$('.register_box_bottom').click(function(){
		if(parseInt($('.register_box_absolute').css('top')) < 0)
		{
			$('.register_box_absolute').animate({ top: '0' }, 600, 'easeOutQuint');
			$('.register_box_bottom').css({'background-position':'0px -28px'});
		}
		else
		{
			$('.register_box_absolute').animate({ top: '-211px' }, 600, 'easeOutQuint');
			$('.register_box_bottom').css({'background-position':'0px 0px'});
		}
	});
	
	$(document).bind('click', function (e) {
		if(parseInt($('.tools').css('left')) == 0)
		{
			$('.tools').animate({ left: '-192px' }, 600, 'easeOutQuint');
			$('.tools_toogle').css({'background-position':'0px 0px'});
		}
	});

	$('.colorpicker').bind('click', function(e) {
		e.stopPropagation();
	});

	$('.tools').bind('click', function(e) {
		e.stopPropagation();
	});
	/* End Tools options */

	
	
	/* SideBar Menu */
	$(".side_bar_menu a").hover(
		function () {
				if (!($(this).hasClass("selected")))
				{
					$('span', this).stop().animate({paddingLeft: 15}, 800, 'easeOutQuint');
				}
			},
		function () {
			if (!($(this).hasClass("selected")))
			{
				$('span', this).stop().animate({paddingLeft: 0}, 800, 'easeOutQuint');
			}
		}
	);
	
	//Tour and shortcodes tabs
	$('#tabs').tabs({ fx: { opacity: 'toggle', duration:'fast'} });
	
	// Inputs set default value
	$('input[title], textarea').bind({
        focus: function() {
            if ($(this).attr('title') == $(this).attr('value')) {
                $(this).attr('value', '');
            }
        },
        blur: function() {
            if ($(this).attr('value') == '') {
                $(this).attr('value', $(this).attr('title'));
            }
        }
    });
	

	/*Slider Paggination Magic Line*/
	var $el, leftPos, newWidth;
	$("#slides .pagination").append('<li class="magic-line"></li>');
	var $magicLinePaggination = $(".magic-line");
	/*End Slider Paggination Magic Line*/
	
	/*First apparition of slide tab*/
	if ( $.browser.msie && parseInt($.browser.version) > 7 ||  !$.browser.msie) {
		var $current_slide = $('#slides .slides_container div.slide').eq(0);
		$current_slide.children('div.text').hide();
		$current_slide.children('div.image').hide();
	}
	/*END First apparition of slide tab*/
	
	$('#slides').slides({
		preload: true,
		preloadImage: 'images/loading.gif',
		generatePagination: false,
		play: 5000,
		pause: 2500,
		hoverPause: true,
		animationStart: function(current){
			/*Hide Slider Tab for make apparition effect on animationComplete*/
			if ( $.browser.msie && parseInt($.browser.version) > 7 ||  !$.browser.msie) {
				curr = (current == 4 ? 1 : (current + 1)) - 1;
				var $current_slide = $('.slides_container div.slide').eq(curr);
				$current_slide.children('div').hide();
				$current_slide.children('image').hide();
			}
			/*END Hide Slider Tab for make apparition effect on animationComplete*/
			
			/*Slider Paggination Magic Line*/
			$el = $(".li_"+(current == 4 ? 1 : (current + 1)));
			leftPos = $el.position().left;
			newWidth = $el.parent().width();
			$magicLinePaggination.stop().animate({
				left: leftPos,
				width: newWidth
			});
			/*END Slider Paggination Magic Line*/
		},
		animationComplete: function(current){
			/*Apparition effect of Slider Tab*/
			if ( $.browser.msie && parseInt($.browser.version) > 7 ||  !$.browser.msie) {
				var $current_slide = $('.slides_container div.slide').eq(curr);
				$current_slide.children('div.text').css('position','relative').css( 'left', '1000px' );
				$current_slide.find('h1').css('position','relative').css( 'left', '1000px' );
				$current_slide.find('p').css('position','relative').css( 'left', '1000px' );
				$current_slide.children('div.image').css('position','relative').css( 'left', '500px' );
				$current_slide.children('div.image.big').css('position','relative').css( 'left', '1000px' );
				$current_slide.children('div.text').show().animate({ left: '0' }, 600, 'easeOutQuint');
				$current_slide.find('h1').animate({ left: '0' }, 800, 'easeOutQuint');
				$current_slide.find('p').animate({ left: '0' }, 800, 'easeOutQuint');
				$current_slide.children('div.image').show().delay(200).animate({ left: '0' }, 600, 'easeOutQuint');
			}
			/*END Apparition effect of Slider Tab*/
		},
		slidesLoaded: function(){
			/*First apparition of slide tab*/
			if ( $.browser.msie && parseInt($.browser.version) > 7 ||  !$.browser.msie) {
				$current_slide.children('div.text').css('position','relative').css( 'left', '1000px' );
				$current_slide.find('h1').css('position','relative').css( 'left', '1000px' );
				$current_slide.find('p').css('position','relative').css( 'left', '1000px' );
				$current_slide.children('div.image').css('position','relative').css( 'left', '500px' );
				$current_slide.children('div.text').show().animate({ left: '0' }, 600, 'easeOutQuint');
				$current_slide.find('h1').animate({ left: '0' }, 800, 'easeOutQuint');
				$current_slide.find('p').animate({ left: '0' }, 800, 'easeOutQuint');
				$current_slide.children('div.image').show().delay(200).animate({ left: '0' }, 600, 'easeOutQuint');
			}
			/*END First apparition of slide tab*/
		}
	});
});




$(document).ready(function(){
	
	preparingTooltips();
	preparingMagnificGlass();
	
	/* Tour page pricing circle */
	if ($(".price_circle")){
		if ( $.browser.msie && parseInt($.browser.version) > 8 ||  !$.browser.msie) {
			var angle = 0;
			setInterval(function(){
				  angle+=3;
				 $(".price_circle").rotate(angle);
			},50);
		}
	}
	
	/* Tour page image list */
	$('.image_list li').hover(
		function()
		{
			var anchor = $('a.magnific-glass-small', this);
			var offset = $(anchor).children('img').offset();
			$('<a class="zoom"><img src="images/zoom.png" alt="" /></a>').appendTo(anchor).css({
				dispay:'block',
				height:$(anchor).children('img').height(), 
				width:$(anchor).children('img').width(),
				'top':offset.top,
				'left':offset.left,
				padding:0}).show();
			$('.button', this).css({backgroundColor: '#ced5e0', color: '#4d5769'});
		},
		function()
		{
			$('.zoom').stop(true, true).fadeOut(500, function(){$(this).remove()});
			$('.button', this).css({backgroundColor: '#f2f4f7', color: '#7f8590'});
		}
	);
	
	/* Tour Page Magic Line */
	$(".tour_tabs ul").append('<li class="magic-line"></li>');
	$(".tour_tabs ul li a").click(function() {
		$(".magic-line").stop().animate({
			left: $(this).position().left
		})
	});
	
	/*Pricing Table adding row class on even td*/
	$(".pricing_table td.pricing_title:even").addClass("grey");
	$(".pricing_table tbody tr:even").addClass("grey");
	
	/*Pricing Table pricing cicle*/
	if ($(".price_bg_active")){
		if ( $.browser.msie && parseInt($.browser.version) > 8 ||  !$.browser.msie)
		{
			var angle = 3;
			setInterval(function(){
				$(".price_bg_active").rotate(angle);
				//$(".price_bg").rotate(angle);
			},50);
		}
	}
	
	/* Homepage companies logo */
	$("ul.companies li").hover(function() { 
		var thumbOver = $(this).find("img").attr("src"); 
		$(this).find("a.thumb").css({'background' : 'url(' + thumbOver + ') no-repeat center bottom'});
		$(this).find("span").stop().fadeTo('normal', 0 , function() {
			$(this).hide()
		}); 
	} , function() {
		$(this).find("span").stop().fadeTo('normal', 1).show();
	});
	
	/* Footer social */
	$(".social a").hover(function() { 
		var thumbOver = extractUrl($(this).find("span.image").css("background-image"));
		$(this).find("span.image").stop().animate({backgroundPosition: '0 -32px'}, 600, 'easeOutQuint') ;
	} , function() {
		$(this).find("span.image").stop().animate({backgroundPosition: '0 0'}, 600, 'easeOutQuint') ;
	});
	
	
	/* Menu and logo animation: drop from top */
	$(".menu").animate({top:'10px'},800,'easeOutCirc').animate({top:'0'},200,'easeOutCirc');
	$(".logo").animate({left:'10px'},800,'easeOutCirc').animate({left:'0'},200,'easeOutCirc');

	/* Menu slide down and hide */
	$(".menu>ul>li").hover(
	  function () {
		$(this).find("ul.children").stop(true, true).animate({ "height": "show", "opacity": "show" }, 320 );
	  }, 
	  function () {
		$(".menu ul.children").stop(true, true).animate({ "height": "hide", "opacity": "hide" }, 320 );
	  }
	);
	

	/*Shortcodes*/
	$(".bordered_box tr:even").addClass("grey");
	$(".toggle_container").hide();
	$(".toggle").click(function(){
		$(this).toggleClass("toggle_active").next().slideToggle("slow");
	});
	$(".opened_toggle").trigger('click');
	/*End Shortcodes*/
	
	/*Pretyphoto*/
		$("a[rel^='prettyPhoto']").prettyPhoto({ animationSpeed:'slow',theme:'facebook',slideshow:2000});
	/*End Pretyphoto*/
	
	/*Easing sliders arrows prev and next */
	/*Prev arrow*/
	$(".slide_prev").find(".prev").hover(
		function () {
			$(".slide_prev").find(".prev_first").clearQueue().stop().delay(80).animate({left: '-50px', opacity: 0}, 300, "easeOutSine");
			$(".slide_prev").find(".prev_last").clearQueue().stop().delay(80).animate({opacity: 0, left: '50px'}, 10).animate({left: '0px', opacity: 1}, 300, "easeOutSine");
		},
		function () {
			$(".slide_prev").find(".prev_first").clearQueue().stop().animate({left: '50px', opacity: 0}, 10).animate({opacity: 1, left: '0px'}, 10);
			$(".slide_prev").find(".prev_last").clearQueue().stop().animate({left: '0px', opacity: 1}, 0).animate({opacity: 0}, 250);
	});
	/*Next arrow*/
	$(".slide_next").find(".next").hover(
	function () {
		$(".slide_next").find(".next_first").clearQueue().stop().delay(80).animate({left: '50px', opacity: 0}, 300, "easeOutSine");
		$(".slide_next").find(".next_last").clearQueue().stop().delay(80).animate({opacity: 0, left: '-50px'}, 10).animate({left: '0px', opacity: 1}, 300, "easeOutSine");
	},
	function () {
		$(".slide_next").find(".next_first").clearQueue().stop().animate({left: '50px', opacity: 0}, 0).animate({opacity: 1, left: '0px'}, 10);
		$(".slide_next").find(".next_last").clearQueue().stop().animate({left: '0px', opacity: 1}, 0).animate({opacity: 0}, 250);
	});
		
});
	
function extractUrl(input)
{
	return input.replace(/"/g,"").replace(/url\(|\)$/ig, "");
}
	
function preparingTooltips()
{
	var targets = {'button_download':{'ref':'button_download', 'tooltip':'download_tooltip'}, 'button_call':{'ref':'button_call', 'tooltip':'call_tooltip'}};

	for(var target in targets)
	{
		$('.' + target).hover(function(){
			var id = $(this).attr('class');
			var obj = targets[id];
			var tooltip = $('#' + obj.tooltip);
			var settings = {'target':$(this), 'position':'middle', 'side':'top', 'endOffset':0, 'duration':150};
			tooltip.showToolTip(settings);
			var ref = $('.' + obj.ref);
		},
		function(){
			var id = $(this).attr('class');
			var obj = targets[id];
			var tooltip = $('#' + obj.tooltip);
			tooltip.hideToolTip({'side':'top'});
			var ref = $('.' + obj.ref);
		});
	}	
}

	

function preparingMagnificGlass()
{
	$('a.magnific-glass').hover(
        function()
        {
			var offset = $(this).children('img').offset();
			if($(this).hasClass('video'))
			{
				var image = 'images/zoom_icon_video.png';
			}
			else
			{
				var image = 'images/blog_zoom_icon.png';
			}
            $('<a class="zoom"><img src="'+image+'" alt="" /></a>').appendTo(this).css({
				height:$(this).children('img').height(), 
				width:$(this).children('img').width(),
				'top':offset.top,
				'left':offset.left,
				padding:0}).show();
        },
        function()
        {
			$('.zoom').stop(true, true).fadeOut(500, function(){jQuery(this).remove()});
        }
    );
}


(function($) {
	$.fn.hideToolTip = function(settings){
		settings = $.extend({
			duration:100,
			side:'bottom',
			startOffset:30
		}, settings);
		var obj = $(this);
		var pos = obj.position();
		obj.stop(false, true);
		if(settings.side == 'bottom')
			end = pos.top + settings.startOffset;
		else if(settings.side == 'top')
			end = pos.top - settings.startOffset;
		obj.animate({'opacity':0.0, 'top':end}, settings.duration, function(){
			obj.css('display', 'none');
		});
	}
	$.fn.showToolTip = function(settings){
		settings = $.extend({
			target:null,
			duration:100,
			position:'middle',
			side:'bottom',
			cornerOffset:20,
			startOffset:30,
			endOffset:0
		}, settings);
		
		if(settings.target == null)
			return;
		
		var obj = $(this);	
		var width = obj.width();
		var targetWidth = settings.target.width();
		var targetHeight = settings.target.height();
		var targetPos = settings.target.offset();
		obj.stop(false, true);
		obj.css({'opacity':0, 'display':'block'});
		var start = 0;
		var left = 0;
		
		if(settings.side == 'bottom')
			start = targetPos.top + targetHeight + settings.startOffset;
		else if(settings.side == 'top')
			start = targetPos.top - obj.height() - settings.startOffset;
			
		if(settings.position == 'middle')
			left = targetPos.left + targetWidth / 2 - width / 2;
		else if(settings.position == 'left')
			left = targetPos.left - settings.cornerOffset;
		else if(settings.position == 'right')
			left = targetPos.left - width + targetWidth + settings.cornerOffset;

		obj.offset({'top':start, 'left':left});
		var end = 0;	
		var pos = obj.position();

		if(settings.side == 'bottom')
			end = pos.top - settings.startOffset + settings.endOffset;
		else if(settings.side == 'top')
			end = pos.top + settings.startOffset - settings.endOffset;

		obj.animate({'opacity':1.0, 'top':end}, settings.duration);
	};

})(jQuery);